package com.neuedu.JDBCDemoWeek05;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Level4Project01Test01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/demo02";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        Statement stmt = conn.createStatement();
        //(1)
        String sql1 = "insert into Users values ('张三','888888','zhangsan@126.com',19861011,1200)";
        String sql2 = "insert into Users values ('李四','999999','lisi@126.com',19881023,700)";
        String sql3 = "insert into Users values ('王五','777777','wangwu@126.com',19900911,2150)";
        stmt.executeUpdate(sql1);
        stmt.executeUpdate(sql2);
        stmt.executeUpdate(sql3);

        //(2)
        String sql4 = "update Users set birthday=now() where name like '张三';";
        stmt.executeUpdate(sql4);

        //(3)
        String sql5 = "delete from Users where name like '王五' ";
        stmt.executeUpdate(sql5);
        //(4)
        String sql6 = "select * from Users where birthday>1987";
        ResultSet rs = stmt.executeQuery(sql6);
        List<Level4Project01Users> users=new ArrayList<>();
        while (rs.next()){
            Level4Project01Users users1=new Level4Project01Users();
            users1.setName(rs.getString("name"));
            users1.setPwd(rs.getString("pwd"));
            users1.setEmail(rs.getString("email"));
            users1.setBirthday(rs.getString("birthday"));
            users1.setMoney(rs.getInt("money"));
            users.add(users1);
        }
        users.forEach(System.out::println);
        stmt.close();
        conn.close();
    }
}
